@extends('layouts.master')

@section('title')
    Partidos
@stop

@section('content')
<div class="row">
	<div class="col-lg-12">
		<section class="panel">
            <div class="table-responsive">
            	<table class="table table-striped m-b-none" data-ride="datatables">
					<thead>
						<tr>
		                	<th colspan="10">
			                   	<div class="row">                        
		    			            <div class="col-sm-8">
		                				<div class="select filter m-b-small m-t-small" data-resize="auto">
		                					<label class="col-lg-6 control-label" style="padding-left: 0px; width: 100px; padding-top: 7px;" for="matchTypeSelector">Tipo de partido  </label>
		                					<select id="matchTypeSelector" name="matchTypeSelector" style="width: 129px;">
									        	<option></option>
									            @foreach($matchTypes as $key => $value)
									            <option value="{{ $value->name }}">{{ $value->name }}</option>
									            @endforeach
									    	</select>
			                           	</div>
		                        	</div>
		                      	</div>
		                    </th>
		                </tr>
						<tr>
							<th>ID</th>
							<th>Fecha</th>
							<th>Cancha</th>
							<th>Ubicación</th>
							<th>Tipo de Partido</th>
							<th>Jugadores Invitados</th>
							<th>Jugadores Sí</th>
							<th>Jugadores No</th>
							<th>Cancelado?</th>
							<th>Admin User</th>
							<!-- <td>Actions</td>-->
						</tr>
					</thead>
					<tbody>
					<!-- display stored matches -->
					@foreach($matches as $key => $value)
						<tr>
							<td>{{ $value->id }}</td>
							<td>{{ (new DateTime($value->date))->format('d/m/Y') }}</td>
							<td><a href="{{ URL::to('fields/' . $value->field->id .'/edit') }}">{{ $value->field->name }}</a></td>
							<td>{{ $value->field->town }}</td>
							<td><a href="{{ URL::to('matchTypes/' . $value->type->id .'/edit') }}">{{ $value->type->name }}</a></td>
							<td class="invitados">{{ $value->guestsAmount }}</td>
							<td class="invitadosSi">{{ $value->guestsConfirmedAmount }}</td>
							<td class="invitadosNo">{{ $value->guestsNotGoingAmount }}</td>
							<td class="cancelados"><input style="height: auto; width: auto;" type="checkbox" disabled="true" <?php if($value->cancelled) echo "checked='checked'" ?> /></td>
							<td>{{ $value->admin->name.' '.$value->admin->lastname }}</td>
				
							<!-- we will also add show, edit, and delete buttons 
							<td>
								<a class="btn btn-small btn-success" href="{{ URL::to('matches/' . $value->id) }}">Mostrar detalle Partido</a>
							</td>-->
						</tr>
					@endforeach
					</tbody>
					<tfoot>
						<tr>
							<td><b>TOTALES</b></td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
							<td class="invitadosTotal"></td>
							<td class="invitadosSiTotal"></td>
							<td class="invitadosNoTotal"></td>
							<td class="canceladosTotal"></td>
							<td></td>
						</tr>
					</tfoot>
				</table>
            </div>
    	</section>
	</div>
</div>
@stop

@section('scripts')
<script type="text/javascript">
	$.fn.dataTable.ext.search.push(
		function( settings, data, dataIndex ) {
			var matchTypeSelected = $('#matchTypeSelector').val();
			var matchType = data[4];
		 
			if (matchTypeSelected == matchType || matchTypeSelected == "") {
		    	return true;
			}
			return false;
		}
	);

	$(document).ready(function() {
		// datatable
		var oTable = $("[data-ride='datatables']").DataTable({
			"sDom": "<'row'<'col-sm-6'l><'col-sm-6'f>r>t<'row'<'col-sm-6'i><'col col-sm-6'p>>",
			"sPaginationType": "full_numbers",
			"language": {
		    	"lengthMenu": "Mostrar _MENU_ registros por página",
		        "zeroRecords": "No se encontraron resultados",
		        "info": "Mostrando página _PAGE_ de _PAGES_",
		        "infoEmpty": "No hay datos para mostrar",
		        "infoFiltered": "(filtrado de un total de _MAX_ registros)"
			}
		});

		findTotals();
	
		$('#matchTypeSelector').change( function() {
			oTable.draw();
			findTotals();
	    });
	});

	function findTotals() {
		var invitados_total = 0;
        var invitados_si_total = 0;
        var invitados_no_total = 0;
        var cancelados_total = 0; 

        $("tbody tr").each(function() {
	        invitados_total += Number($(this).find("td.invitados").html());
	        invitados_si_total += Number($(this).find("td.invitadosSi").html());
	        invitados_no_total += Number($(this).find("td.invitadosNo").html());
	        if($(this).find("td.cancelados").find("input").prop("checked")) 
	        	cancelados_total ++;
	    });

	    $("tfoot td.invitadosTotal").html(invitados_total);
	    $("tfoot td.invitadosSiTotal").html(invitados_si_total);
	    $("tfoot td.invitadosNoTotal").html(invitados_no_total);
	    $("tfoot td.canceladosTotal").html(cancelados_total);
	}
</script>
@stop